package net.jeeframework.fileuplod.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.PropertiesConfiguration;

public class FileSystemConfig {

	public static Configuration FILE_SYSTEM_CONFIG;

	public static String FILE_SAVE_DIR = "file";
	public static String IMAGE_SAVE_DIR = "image";
	public static String FLASH_SAVE_DIR = "flash";
	public static String MEDIA_SAVE_DIR = "media";

	public static final List<String> FILES = new ArrayList<String>();
	public static final List<String> IMAGES = new ArrayList<String>();
	public static final List<String> FLASHS = new ArrayList<String>();
	public static final List<String> MEDIAS = new ArrayList<String>();
	public static final List<String> RESOURCES = new ArrayList<String>();

	static {
		try {
			if (FILE_SYSTEM_CONFIG == null) {
				FILE_SYSTEM_CONFIG = new PropertiesConfiguration(ClassLoader.getSystemResource(Constants.CONFIG_FILE_PATH));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		String file = FILE_SYSTEM_CONFIG.getString("resourceType.file.extensions.allowed");
		String image = FILE_SYSTEM_CONFIG.getString("resourceType.image.extensions.allowed");
		String flash = FILE_SYSTEM_CONFIG.getString("resourceType.flash.extensions.allowed");
		String media = FILE_SYSTEM_CONFIG.getString("resourceType.media.extensions.allowed");

		String[] files = file != null ? file.split(",") : null;
		String[] images = image != null ? image.split(",") : null;
		String[] flashs = flash != null ? flash.split(",") : null;
		String[] medias = media != null ? media.split(",") : null;

		for (String e : files) {
			FILES.add(e);
		}

		for (String e : images) {
			IMAGES.add(e);
		}

		for (String e : flashs) {
			FLASHS.add(e);
		}

		for (String e : medias) {
			MEDIAS.add(e);
		}

		RESOURCES.addAll(FILES);
		RESOURCES.addAll(FLASHS);
		RESOURCES.addAll(IMAGES);
		RESOURCES.addAll(MEDIAS);
	}

	public static Configuration getConfig() {
		return FILE_SYSTEM_CONFIG;
	}

	public static String getSaveDir(int fileType) {
		return FILE_SYSTEM_CONFIG.getString("base.save.dir");
	}

	public static String getSaveDir() {
		String dir = null;
		dir = getConfig().getString("file.save.dir");
		return dir;
	}

	public static String getDirByFileType(int fileType) {

		String dir = null;

		switch (fileType) {

		case Constants.FILE_TYPE_FILE:
			dir = getConfig().getString("file.save.dir") == null ? Constants.FILE_SAVE_DIR : getConfig().getString("file.save.dir");
			break;
		case Constants.FILE_TYPE_IMAGE:
			dir = getConfig().getString("image.save.dir") == null ? Constants.IMAGE_SAVE_DIR : getConfig().getString("image.save.dir");
			break;
		case Constants.FILE_TYPE_FLASH:
			dir = getConfig().getString("flash.save.dir") == null ? Constants.FLASH_SAVE_DIR : getConfig().getString("flash.save.dir");
			break;
		case Constants.FILE_TYPE_MEDIA:
			dir = getConfig().getString("media.save.dir") == null ? Constants.MEDIA_SAVE_DIR : getConfig().getString("media.save.dir");
			break;
		default:
			dir = getConfig().getString("file.save.dir") == null ? Constants.FILE_SAVE_DIR : getConfig().getString("file.save.dir");
			break;
		}

		return dir;

	}
	
	

}
